﻿using System.Data;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneDealerList : UserControl
    {
        private VodafoneController _vodafoneController;

        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                _vodafoneController.ShowingDealerImport += _vodafoneController_ShowingDealerImport;
                _vodafoneController.ShowingDealerFilter += _vodafoneController_ShowingDealerFilter;
            }
        }

        #endregion

        public VodafoneDealerList()
        {
            InitializeComponent();
        }

        private bool ContainsField(string columnName)
        {
            foreach (GridColumn column in gridView.Columns)
                if(column.FieldName == columnName)
                    return true;
            return false;
        }

        private void RetrieveFields(VodafoneDealerDataSet dataset)
        {
            foreach (GridColumn column in gridView.Columns)
                column.Visible = dataset.Dealer.Columns.Contains(column.FieldName);
            foreach (DataColumn column in dataset.Dealer.Columns)
                if(!ContainsField(column.ColumnName))
                {
                    GridColumn gridColumn = new GridColumn();
                    gridColumn.Name = "col" + column.ColumnName;
                    gridColumn.Caption = column.ColumnName;
                    gridColumn.FieldName = column.ColumnName;
                    gridColumn.Visible = true;
                    gridColumn.OptionsColumn.AllowEdit = false;
                    gridColumn.VisibleIndex = gridView.Columns.Count;
                    gridView.Columns.Add(gridColumn);
                }
            gridView.Columns.ColumnByFieldName("IdDealer").Visible = false;
        }

        private void FillData(VodafoneDealerDataSet dataset)
        {
            RetrieveFields(dataset);
            vodafoneDealerDataSetBindingSource.DataSource = null;
            vodafoneDealerDataSetBindingSource.DataSource = dataset;
            vodafoneDealerDataSetBindingSource.DataMember = dataset.Dealer.TableName;
            gridView.RefreshData();
        }

        void _vodafoneController_ShowingDealerImport(object sender, System.EventArgs e)
        {
            VodafoneDealerImport window = new VodafoneDealerImport();
            window.VodafoneController = _vodafoneController;
            if (DialogResult.OK != window.ShowDialog())
                return;
            VodafoneDealerDataSet dataSet = _vodafoneController.ImportDealer(window.DealerSetupId, window.FileName, window.YearMonth);
            FillData(dataSet);
        }

        void _vodafoneController_ShowingDealerFilter(object sender, System.EventArgs e)
        {
            VodafoneDealerFilter window = new VodafoneDealerFilter();
            window.VodafoneController = _vodafoneController;
            if (DialogResult.OK != window.ShowDialog())
                return;
            VodafoneDealerDataSet dataSet = _vodafoneController.FilterDealer(window.BusinessType, window.BusinessLine, window.YearMonth);
            FillData(dataSet);
        }


    }
}
